package org.smarthings;

import java.text.SimpleDateFormat;

public class Message implements Comparable<Message> {
	private String uri;
	private byte[] payload;
	private String protocol;
	private long time;
	private String accept;
	private String contentType;
	private int response;
	private String method;

	Message(byte[] payload, String protocol, String uri, String contentType, String accept, int response, String method) {
		this.payload = payload;
		this.protocol = protocol;
		this.uri = Utils.normalizeUri(uri);
		this.contentType = contentType;
		this.accept = accept;
		this.response = response;
		this.time = System.currentTimeMillis();
		this.method = method;
	}

	public Message copy(String protocol) {
		return new Message(payload, protocol, uri, contentType, accept, response, method);
	}

	public static Message createSimpleMessage(byte[] payload, String protocol, String uri, String contentType) {
		return new Message(payload, protocol, uri, contentType, contentType, Response.OK, null);
	}

	public static Message createReciever(byte[] payload, String protocol, String uri, String contentType, int response) {
		return new Message(payload, protocol, uri, contentType, contentType, response, null);
	}

	public static Message createSender(byte[] payload, String protocol, String uri, String contentType, String accept) {
		return new Message(payload, protocol, uri, contentType, accept, Response.OK, null);
	}

	public static Message createSender(byte[] payload, String protocol, String uri, String contentType,
			String accept, String method) {
		return new Message(payload, protocol, uri, contentType, accept, Response.OK, method);
	}

	public String toString() {
		StringBuffer result = new StringBuffer(50);
		result.append("Message = ").append(new String(payload)).append("\n\tContentType = ").append(contentType)
				.append("\n\tAccept = ").append(accept).append("\n\tProtocol = ").append(protocol).append("\n\tURI = ")
				.append(uri).append("\n\tDate = ").append(new SimpleDateFormat("HH:mm:ss d/m/yy ").format(time))
				.append("\n\tResponseCode = ").append(response).append("\n\tMethod = ").append(method);
		return result.toString();
	}

	public boolean equals(Object o) {
		if (o instanceof Message) {
			Message m = (Message) o;
			return protocol.equals(m.protocol) && uri.equals(m.uri);
		}

		return super.equals(o);
	}

	public int hashCode() {
		return protocol.hashCode() + uri.hashCode();
	}

	public int compareTo(Message m) {
		int pr = protocol.compareTo(m.protocol);
		int ur = uri.compareTo(m.uri);
		return pr != 0 ? pr : ur != 0 ? ur : 0;
	}

	public byte[] getPayload() {
		return payload;
	}

	public String getProtocol() {
		return protocol;
	}

	public String getUri() {
		return uri;
	}

	public String getContentType() {
		return contentType;
	}

	public String getAccept() {
		return contentType;
	}

	public int getResponseCode() {
		return response;
	}

	public String getMethod() {
		return method;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}
}
